package com.zachary.es;

import com.zachary.es.hadoop.map.SearchMapper;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.elasticsearch.hadoop.mr.EsInputFormat;

import java.io.IOException;

/**
 * <pre>
 * Created with IntelliJ IDEA.
 * User: zachary.
 * Date: 2014/7/15
 * Time: 17:29
 * PC：windows'IDEA in company <br>
 * </pre>
 *
 * @author zachary.
 */
public class SearchApp {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        conf.set("es.nodes", "192.168.1.109");
        conf.set("es.port", "9200");
        conf.set("es.resource", "medicine/basic");
        conf.set("es.query", "{'query':{'term' : { 'postTitle' : '宝宝' }}}");
        conf.set("es.input.json", "yes");
        Job job = Job.getInstance(conf);
        job.setJobName("esSearch");
        job.setJarByClass(SearchMapper.class);
        job.setInputFormatClass(EsInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        FileOutputFormat.setOutputPath(job, new Path("/user/hadoop/es/es" + System.currentTimeMillis()));
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setMapperClass(SearchMapper.class);
        job.setNumReduceTasks(0);
        System.exit(job.waitForCompletion(true) ? 0 : -1);
    }
}
